﻿using GameProtocol.Dto;
using RPGGameServer.Dao;
using ServerFrame;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RPGGameServer.Cache
{
    /// <summary>
    /// 数据层提供的账号模型
    /// </summary>
    //public class ACCOUNT
    //{
    //    //账号ID
    //    public int id;
    //    //账号
    //    public string account;
    //    //密码
    //    public string password;
    //}
    public class AccountCache:ICache
    {
        /// <summary>
        /// 在线用户列表<UserToken, 账户名称>
        /// </summary>
        Dictionary<UserToken, string> dicOnlines = new Dictionary<UserToken, string>();

        /// <summary>
        /// 所有在线用户账号信息<账户名称, 账户对象>
        /// </summary>
        Dictionary<string, ACCOUNT> dicAccounts = new Dictionary<string, ACCOUNT>();

        /// <summary>
        /// 创建账号，把账号缓存起来
        /// </summary>
        public void Add(UserToken token, AccountDto dto)
        {
            ACCOUNT a = new ACCOUNT();
            a.account = dto.account;
            a.password = dto.password;
            dicAccounts.Add(dto.account, a);

            //TODO: 写入数据库
            a.Add();
        }

        /// <summary>
        /// 获取在线账户id
        /// </summary>
        /// <returns></returns>
        public int GetAccountId(UserToken token)
        {
            //判断是否在线
            if (IsOnline(token))
            {
                return dicAccounts[dicOnlines[token]].id;
            }

            return -1;
        }

        /// <summary>
        /// 判断用户是否在线
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        public bool IsOnline(UserToken token)
        {
            return dicOnlines.ContainsKey(token);
        }

        /// <summary>
        /// 判断用户是否被注册
        /// </summary>
        /// <param name="account"></param>
        /// <returns></returns>
        public bool HasAccount(string account)
        {
            //TODO:查询数据库
            return dicAccounts.ContainsKey(account);
        }

        /// <summary>
        /// 用户上线操作
        /// </summary>
        /// <param name="token"></param>
        /// <param name="account"></param>
        public void Online(UserToken token, string account)
        {
            if (dicOnlines.ContainsKey(token))
            {
                dicOnlines[token] = account;
                return;
            }
            dicOnlines.Add(token, account);
        }

        /// <summary>
        /// 用户下线操作
        /// </summary>
        /// <param name="token"></param>
        public void OffLine(UserToken token)
        {
            //先判断用户是否在线
            if (IsOnline(token))
            {
                string account = dicOnlines[token];
                if (dicAccounts.ContainsKey(account))
                {
                    dicAccounts.Remove(account);
                }
                dicOnlines.Remove(token);
            }
        }

        /// <summary>
        /// 判断账号密码是否匹配
        /// </summary>
        /// <param name="account"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public bool Matching(string account, string password)
        {
            return dicAccounts[account].password.Equals(password);
        }
    }
}
